/**
 * splash页
 * 由 WangYu 创建
 * 日期 2017/9/6
 */
import React, {Component} from 'react';
import {
    StyleSheet,
    Text,
    View,
    Image,
    ToastAndroid, StatusBar, TouchableNativeFeedback, Dimensions,
} from 'react-native';
import {Actions, ActionConst} from "react-native-router-flux";
import Api from "../../util/Api";
import NetUtils from "../../util/NetUtils";
import Toast from "teaset/components/Toast/Toast";
export default class SplashPage extends Component {

    constructor(props) {
        super(props);
        StatusBar.setTranslucent(true);
        StatusBar.setBackgroundColor('transparent');
    }

    render() {
        let {width, height} = Dimensions.get('window');
        return (
            <View style={styles.container}>
                <StatusBar translucent={true} barStyle="light-content"/>
                <Image style={{width: width, height: height}}
                       source={require('../../../images/splash.png')}/>
            </View>
        );
    }

    componentDidMount() {
        //倒计时两秒之后进入首页
        setTimeout(() => {
            Actions.MainPage({type: 'reset'});
        }, 2000);
        //获取本地存贮的userid和token
        storage.load({
            key: 'userInfo'
        }).then(userInfo => {
            if (userInfo.userId === '' || userInfo.token === '') {
                return;
            }
            let params = 'userId=' + userInfo.userId + '&token=' + encodeURI(userInfo.token);
            NetUtils.post(Api.reLogin, params, (result) => {
                if (result.code === 1) {
                    //登录成功
                    Api.isLogin = true;
                    Api.userInfo = result.object;
                    //本地存储userid和token
                    storage.save({
                        key: 'userInfo',  // 注意:请不要在key中使用_下划线符号!
                        data: {
                            userId: result.object.userId,
                            token: result.object.token
                        },
                    });
                }
            },null, true);
        }).catch(err => {
            //如果没有找到数据且没有sync方法，
            //或者有其他异常，则在catch中返回
            console.log(err.toString());
        })
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
    },
});
